﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SHFST.Agents
{
   class Salmon : Agent
   {
      const float SPEED_MIN = -0.15f;
      const float SPEED_MAX = -.35f;
      const float SPEED_CHANGE = 0.0025f;

      int screenHeight;
      float speed;
      public Bullet bullet;
      public int ammo;
      public int score;
      SoundEffectInstance shootSound;

      public void Load (ContentManager content,
                        Texture2D newTexture,
                        Point newFrameSize,
                        Point newSheetSize,
                        Point newPos,
                        int newScreenHeight,
                        Bullet newBullet)
      {
         base.Load (newTexture, newFrameSize, newSheetSize, newPos);

         shootSound = content.Load<SoundEffect> (".\\Sounds\\Agents\\SalmonShoot").CreateInstance ();

         screenHeight = newScreenHeight;
         bullet = newBullet;
         speed = SPEED_MIN;
         life = 5;
         ammo = 20;
         score = 0;
      }

      public bool Update (ref int retSpeed, int elapsedTime)
      {
         if (life <= 0 || ammo <= 0)
         {
            return false;
         }

         base.Update (elapsedTime);

         KeyboardState keyboardState = Keyboard.GetState ();
         if (keyboardState.IsKeyDown (Keys.Right) && speed > SPEED_MAX)
         {
            speed -= SPEED_CHANGE;
         }
         else if (keyboardState.IsKeyDown (Keys.Left) && speed < SPEED_MIN)
         {
            speed += SPEED_CHANGE;
         }

         if (keyboardState.IsKeyDown (Keys.Up) && position.Y > screenHeight * 1 / 4)
         {
            position.Y -= 3;
         }
         else if (keyboardState.IsKeyDown (Keys.Down) && position.Y < screenHeight - sprite.frameSize.Y)
         {
            position.Y += 3;
         }
         position.Y = position.Y;

         if (bullet.kill)
         {
            bullet.Activate (false, Point.Zero);
         }
         else if (bullet.speed != 0)
         {
            bullet.Update (elapsedTime);
         }
         else if (bullet.speed == 0 && keyboardState.IsKeyDown (Keys.Space))
         {
            bullet.Activate (true, new Point (position.X + sprite.frameSize.X, position.Y));
            --ammo;
            shootSound.Play ();
         }

         retSpeed = (int)(speed * elapsedTime);
         return true;
      }

      public override void Draw (SpriteBatch batch)
      {
         base.Draw (batch);

         if (bullet.speed != 0)
         {
            bullet.Draw (batch);
         }
      }
   }
}
